﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace GatewayServer.Common
{
    public class PageHelper
    {
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="pageCount">总页数</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="showLimit">规定多于几页的时候显示</param>
        /// <param name="showCount">显示几个</param>
        /// <returns></returns>
        public string PageBar(int pageCount, int pageIndex, int showCount)
        {
            string result = "";


            //数据数量小于指定数量
            if (pageCount < 3)
            {
                result = "";
            }
            //多于指定数量
            else
            {

                int min = 0;
                int max = 0;

                ///--------------添加前面....显示上一组分页码---------------------
                if (pageIndex >= showCount)
                {
                    result += ForStartFunction((pageIndex / showCount - 1) * showCount + (showCount - 1));

                }
                ////////////////////////////////////--------------显示中间的分页码数字---------------------
                if (pageIndex < showCount)
                {
                    min = 1;
                    max = showCount;
                }
                else if ((pageIndex - 1) % showCount == 0)
                {
                    min = pageIndex / showCount * showCount;// pageIndex - beginMove + 1;//往前移动一位
                    max = min + showCount;
                    if (min < 1) min = 1;
                }
                else
                {
                    min = pageIndex / showCount * showCount;
                    max = min + showCount;
                }
                if (min > pageCount) min = pageCount;
                if (max > pageCount) max = pageCount;
                result += ForFunction(min, max, pageIndex);

                //-------------------添加...翻下一组-----------
                if (max < pageCount)
                {
                    result += ForEndFunction(max + 1);

                }


            }

            return result;
        }
        /// <summary>
        /// 循环
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public string ForFunction(int min, int max, int pageIndex)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = min; i <= max; i++)
            {
                sb.Append("<ul class='pagination'><li><a ");
                string style = pageIndex == i ? "style=\"color:red;background:#B8DFFB;\"" : "";
                sb.Append(style);
                sb.Append(" href=\"javascript:;\" onclick=\"goToPage(" + i + ")\" ");
                // sb.Append(url);
                //sb.Append(i);
                sb.Append("\">");
                sb.Append(i);
                sb.Append("</a></li></ul>&nbsp;");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 结束
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="pageIndex"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public string ForEndFunction(int max)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<ul class='pagination'><li><a ");
            sb.Append(" href=\"javascript:;\" onclick=\"goToPage(" + max + ")\" ");
            //     sb.Append(url);
            //   sb.Append(max);

            sb.Append("\">...");

            sb.Append("</a></li></ul>&nbsp;");

            return sb.ToString();
        }
        /// <summary>
        /// 结束
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="pageIndex"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public string ForStartFunction(int max)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<ul class='pagination'><li><a ");
            sb.Append(" href=\"javascript:;\" onclick=\"goToPage(" + max + ")\" ");
            // sb.Append(url);
            //sb.Append(max);

            sb.Append("\">...");

            sb.Append("</a></li></ul>&nbsp;");

            return sb.ToString();
        }
        public string GetPageHtml(int totalRecordCount, int pageIndex, int pageCount, bool ShowNumCount = true)
        {
            int showCount = 5;//显示多少个页数码
            //int first = 1;
            int prev = pageIndex - 1 == 0 ? 1 : pageIndex - 1;//上一页
            int next = pageIndex == pageCount ? pageCount : pageIndex + 1;//下一页

            if (pageCount < 1) pageCount = 1;
            // if (!url.Contains("?")) url += "?1=1";

            string PageHtml = "<div class=\"Page-foot\">";
            //  PageHtml += " <div class=\"pagrecord\">记录数：" + totalRecordCount + " 条 &nbsp;&nbsp;页数：" + pageIndex + "/" + pageCount + " </div>";
            PageHtml += "<div class=\"PagList\">";
            PageHtml += "<div class=\"pagrecord\"> 记录数：" + totalRecordCount + " 条 &nbsp;&nbsp;页数：" + pageIndex + "/" + pageCount + " </div>";

            PageHtml += "<ul class=\"pagination\"><li><a href =\"javascript:;\" onclick=\"goToPage(1)\"> 首页 </a></li></ul>";
            PageHtml += "<ul class=\"pagination\"><li><a href =\"javascript:;\" onclick=\"goToPage(" + prev + ")\"> 上一页 </a></li></ul>";
            if (ShowNumCount == true)
            {
                PageHtml += PageBar(pageCount, pageIndex, showCount);
            }
            PageHtml += "<ul class=\"pagination\"><li><a href =\"javascript:;\" onclick=\"goToPage(" + next + ")\"> 下一页 </a></li></ul>";
            PageHtml += "<ul class=\"pagination\"><li><a href =\"javascript:;\" onclick=\"goToPage(" + pageCount + ")\"> 尾页 </a></li></ul>";
            if (pageIndex > showCount)
            {
                PageHtml += "<ul class=\"pagination\"><li><input type='number' id='txt_PageNo' name='txt_PageNo' style='width:40px;' />&nbsp;<input type='button' onclick='goToPage(document.getElementById(\"txt_PageNo\").value)' id='but_page' name='but_page' value='Go'/></li></ul>";
            }

            PageHtml += "</div><div  class=\"pagend\">&nbsp;</div> </div>";
            PageHtml += "<div></div>";
            PageHtml += "<script language='javascript'>function goToPage(IPage) {    document.getElementById(\"hid_pageIndex\").value = IPage;  ";
            PageHtml += "$(\"#hid_pageIndex\").parents('form')[0].submit();";
            PageHtml += "  }</script>";
            PageHtml += "<input type=\"hidden\" value=\"1\" id=\"hid_pageIndex\" name=\"hid_pageIndex\" />";
            return PageHtml.ToString();
        }
    }
}